package com.freight.tws.controller.app;

import com.freight.common.core.constant.Constants;
import com.freight.common.core.domain.R;
import com.freight.tws.api.domain.Driver;
import com.freight.tws.api.domain.Vehicle;
import com.freight.tws.domain.DriverBank;
import com.freight.tws.third.AliOcrService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 手机端 - 第三方API
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping(Constants.APP_URL)
public class AppThirdController {

    private final AliOcrService aliOcrService;

    /** 阿里云 - 身份证人像面识别 */
    @PostMapping("/ocr-idcard-face")
    public R ocrIdcardFace(@RequestBody String url) {
        Driver driver = aliOcrService.ocrIdcard(url);
        if (driver == null){
            driver = new Driver();
        }
        return R.ok(driver);
    }

    /** 阿里云 - 身份证国徽面识别 */
    @PostMapping("/ocr-idcard-back")
    public R ocrIdcardBack(@RequestBody String url) {
        Driver driver = aliOcrService.ocrIdcard(url);
        if (driver == null){
            return R.fail("身份证已过期，无法实名...");
        }
        return R.ok();
    }

    /** 阿里云 - 驾驶证主页识别 */
    @PostMapping("/ocr-driver-face")
    public R ocrDriverFace(@RequestBody String url) {
        Driver driver = aliOcrService.ocrCarIdcard(url);
        if (driver == null){
            driver = new Driver();
        }
        return R.ok(driver);
    }

    /** 阿里云 - 行驶证主页识别 */
    @PostMapping("/ocr-vehicle-face")
    public R ocrVehicleFace(@RequestBody String url) {
        Vehicle vehicle = aliOcrService.ocrVehicleFace(url);
        if (vehicle == null){
            vehicle = new Vehicle();
        }
        return R.ok(vehicle);
    }

    /** 阿里云 - 行驶证副页识别 */
    @PostMapping("/ocr-vehicle-back")
    public R ocrVehicleBack(@RequestBody String url) {
        Vehicle vehicle = aliOcrService.ocrVehicleFace(url);
        if (vehicle == null){
            vehicle = new Vehicle();
        }
        return R.ok(vehicle);
    }

    /** 阿里云 - 行驶证副页识别 */
    @PostMapping("/ocr-bank")
    public R ocrBank(@RequestBody String url) {
        DriverBank driverBank = aliOcrService.ocrBank(url);
        if (driverBank == null){
            driverBank = new DriverBank();
        }
        return R.ok(driverBank);
    }

}
